package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

/**
 * Servlet implementation class UserRegistration
 */
public class UserLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserLogin() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("userid") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Landing.jsp");
			dispatcher.forward(request, response);
		} else {

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Home.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String inputEmail = request.getParameter("email");
		String inputPwd = request.getParameter("password");

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			String getUserSql = "SELECT user_id,first_name,email,password FROM users where email='"
					+ inputEmail + "' and password='" + inputPwd + "'";
			rs = stmt.executeQuery(getUserSql);

			// displaying records
			if (rs.next()) {
				HttpSession session = request.getSession();
				session.setAttribute("userid", rs.getObject(1).toString());

				String fname = rs.getObject(2).toString();
				session.setAttribute("username", fname);

				String email = rs.getObject(3).toString();
				session.setAttribute("email", email);

				String respassword = rs.getObject(4).toString();
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Landing.jsp");
				dispatcher.forward(request, response);
			} else {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Home.jsp");
				dispatcher.forward(request, response);
			}
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
		}
	}
}
